package com.mqz.better.mysql.master.slave.mycat.web;

import com.mqz.better.mysql.common.SysUserEntity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *  版权所有 © Copyright 2012<br>
 *
 * @Author： 蒙大拿
 * @Date：2021/10/15 10:44 上午
 * @Description
 * @About： https://github.com/DemoMeng
 */
@RestController
@RequestMapping(value = "/mycat/web")
public class MyCatController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询所有记录
     * @return
     */
    @GetMapping(value = "/queryAll")
    public List<SysUserEntity> queryAll() {
        String sql = "SELECT id,name,user_name,phone,password,create_time,create_user_id,update_time,update_user_id FROM sys_user;";
        List<SysUserEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SysUserEntity.class));
        return list;
    }


    /**
     *
     * 无id则为插入
     * 更新或者插入
     * @param sysUserEntity
     * @return
     */
    @PostMapping(value = "/update")
    public boolean update(@RequestBody SysUserEntity sysUserEntity) {
        int update = 0;
        if(sysUserEntity.getId() != null){
            String sql = "UPDATE sys_user SET name = ?,user_name=?,phone=?,password=?,create_time=?,create_user_id=?,update_time=?,update_user_id=? WHERE id= ?";
             update = jdbcTemplate.update(sql,sysUserEntity.getName(),sysUserEntity.getUserName(),sysUserEntity.getPhone(),sysUserEntity.getPassword(),
                    sysUserEntity.getCreateTime(),sysUserEntity.getCreateUserId(),sysUserEntity.getUpdateTime(),sysUserEntity.getUpdateUserId());
        }else{
            String sql = "INSERT INTO sys_user(name, user_name, phone, password, create_time, create_user_id, update_time, update_user_id) VALUES (?,?,?,?,?,?,?,?)";
            update = jdbcTemplate.update(sql,sysUserEntity.getName(),sysUserEntity.getUserName(),sysUserEntity.getPhone(),sysUserEntity.getPassword(),sysUserEntity.getCreateTime(),sysUserEntity.getCreateUserId(),sysUserEntity.getUpdateTime(),sysUserEntity.getUpdateUserId());
        }
        return update > 0 ? true:false;
    }

    /**
     *
     * @param sysUserId
     * @return
     */
    @GetMapping(value = "/delete")
    public boolean delete(Integer sysUserId) {
        String sql = "DELETE FROM sys_user WHERE id= ?";
        int flag = jdbcTemplate.update(sql,sysUserId);
        return flag>0?true:false;
    }


}
